package lock;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

/**
 * tryLock不用等待，立即相应的策略
 * @author lcx
 *
 */
public class ReentrantTryLockTest {

	public static void main(String[] args) {
		ReentrantLock lock=new ReentrantLock();
		TryLockThread td1=new TryLockThread(lock);
		TryLockThread td2=new TryLockThread(lock);
		td1.start();
		td2.start();
	}
}

class TryLockThread extends Thread
{
	ReentrantLock lock;
	public TryLockThread(ReentrantLock lock)
	{
		this.lock=lock;
	}

	@Override
	public void run()
	{
		if(lock.tryLock())
		{
				System.out.println("start......");
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				lock.unlock();
				System.out.println("end");
			}
	}
}
